package com.lkk.cg.util;

import com.lkk.cg.domain.Privilege;
import com.lkk.cg.domain.Role;
import com.lkk.cg.domain.User;
import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by lkk on 2014/12/5.
 */
public class Installer {

    public static void main(String[] args) {
        Installer installer = new Installer();
        installer.install();
    }

    //初始化数据库数据，包括权限数据，超级用户等
    public void install() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        SessionFactory factory = (SessionFactory) ctx.getBean("sessionFactory");
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            //================添加管理员================
            User user = new User();
            user.setLoginName("admin");
            user.setPassword(DigestUtils.md5Hex("admin"));
            session.save(user);


            //=================添加权限=================

            Privilege menu, privilege1, privilege2, privilege3, privilege4, privilege5;

            //>>用户管理
            menu = new Privilege("用户管理", "/userManage_list", 1);
            privilege1 = new Privilege("用户添加", "/userManage_add", 1);
            privilege2 = new Privilege("用户修改", "/userManage_edit", 1);
            privilege3 = new Privilege("用户删除", "/userManage_delete", 1);
            privilege4 = new Privilege("密码重置", "/userManage_initPassword", 1);
            menu.addChild(privilege1);
            menu.addChild(privilege2);
            menu.addChild(privilege3);
            menu.addChild(privilege4);
            session.save(menu);

            //>>文章管理
            menu = new Privilege("文章管理", "/articleManage_list", 1);
            privilege1 = new Privilege("文章删除", "/articleManage_delete", 1);
            menu.addChild(privilege1);
            session.save(menu);

            //>>角色管理
            menu = new Privilege("角色管理", "/roleManage_list", 1);
            privilege1 = new Privilege("角色添加", "/roleManage_add", 1);
            privilege2 = new Privilege("角色修改", "/roleManage_edit", 1);
            privilege3 = new Privilege("角色删除", "/roleManage_delete", 1);
            privilege4 = new Privilege("设置权限", "/roleManage_setPrivilege", 1);
            menu.addChild(privilege1);
            menu.addChild(privilege2);
            menu.addChild(privilege3);
            menu.addChild(privilege4);
            session.save(menu);

            //>>登陆可以访问的权限
            menu = new Privilege("信息修改", "/user_editProfile", 2);
            session.save(menu);

            menu = new Privilege("我的文章", "/user_articleList", 2);
            privilege1 = new Privilege("文章删除", "/user_articleDelete", 2);
            menu.addChild(privilege1);
            session.save(menu);

            menu = new Privilege("注销", "/user_logout", 2);
            session.save(menu);

            privilege1 = new Privilege("文章上传", "/article_add", 2);
            privilege2 = new Privilege("评论文章", "/article_comment", 2);
            session.save(privilege1);
            session.save(privilege2);

            //=================基础角色=================
            Role role = new Role("basic", "基本角色，只有最基本的权限，不能删除！", true);
            session.save(role);

            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        } finally {
            session.close();
        }
    }
}
